﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using SHomeWorkshop.LunarPresentation.Expressions;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        public static void MoveToPrevious()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            if (curEx is Expressions.SubPanel)
            {
                MessageBox.Show("子面板不支持“前后平移”操作！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;//子面板不支持Copy。
            }

            if (curEx is Expressions.BaseText)
            {
                Expressions.BaseText bt = curEx as Expressions.BaseText;
                if (bt.IsHeaderText && bt.Indent <= 3 && bt.IsCollapsed)
                {
                    Command.MoveCollapsedHeaderTextToPreview(bt);
                    return;
                }
            }

            Command.MoveNormalExToPreview(curEx);

            // 刷新父面板中所有可能存在的树型线。
            if (curEx.ParentPanel != null && curEx.ParentPanel.Direction == Orientation.Vertical)
            {
                curEx.ParentPanel.RefreshTreeLinesOfHeaderText();
            }
        }

        private static void MoveNormalExToPreview(Expressions.Expression curEx)
        {
            if (curEx == null || curEx.ParentPanel == null) return;

            int preVisibleExIndex = -1;
            int curIndex = curEx.ParentPanel.BasePanel.Children.IndexOf(curEx);
            Expressions.Expression preVisibleEx = null;
            for (int i = curIndex - 1; i >= 0; i--)
            {
                Expressions.Expression ex = curEx.ParentPanel.BasePanel.Children[i] as Expressions.Expression;
                if (ex.Visibility == Visibility.Visible)
                {
                    preVisibleEx = ex;
                    preVisibleExIndex = i;
                    break;
                }
            }

            if (preVisibleExIndex == -1 || preVisibleEx == null) return;

            if (preVisibleEx is BaseText == false)
            {
                Command.MoveNormalExToPreviewNormalEx(curEx);
                return;
            }

            Expressions.BaseText preBt = preVisibleEx as Expressions.BaseText;

            List<Expressions.Expression> subIndentExpressions = preBt.GetSubIndentExpressions();

            if (preBt.IsHeaderText && preBt.Indent <= 3 && preBt.IsCollapsed &&
                subIndentExpressions != null && subIndentExpressions.Count > 0)
            {
                Command.MoveNormalExToPreviewCollapsedHeaderText(curEx, preBt);
            }
            else
            {
                Command.MoveNormalExToPreviewNormalEx(curEx);
            }
        }

        private static void MoveNormalExToPreviewCollapsedHeaderText(Expressions.Expression curEx, BaseText preBt)
        {
            MessageBox.Show("因为要跨过一个被折叠的标题文本块，此操作被禁止！",
                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
            return;
        }

        private static void MoveNormalExToPreviewNormalEx(Expressions.Expression curEx)
        {
            SubPanel parentPanel = curEx.ParentPanel;
            int indexOfCurEx = parentPanel.BasePanel.Children.IndexOf(curEx);
            if (indexOfCurEx < 1) return;//已在第一位，无法再前移。

            ModifingItem mi = new ModifingItem();
            Action action = new Action(curEx.ExID, indexOfCurEx, curEx.GetType().Name, indexOfCurEx.ToString(),
                "Index", (indexOfCurEx - 1).ToString(), ActionType.MoveToPreview, curEx.ParentPanel.ExID);

            parentPanel.BasePanel.Children.Remove(curEx);
            curEx.XmlData.MoveToPrevious();
            parentPanel.BasePanel.Children.Insert(indexOfCurEx - 1, curEx);

            mi.Add(action);
            curEx.Editor.Do(mi);

            if (curEx is Expressions.BaseText)
            {
                Expressions.BaseText bt = curEx as Expressions.BaseText;
                if (bt.IsHeaderText)
                {
                    parentPanel.RefreshAutoNumbersOfBaseText();
                }
            }

            Globals.selectedEditor.IsModified = true;
        }

        private static void MoveCollapsedHeaderTextToPreview(BaseText bt)
        {
            MessageBox.Show("当前文本块处于被折叠状态，不允许平移！", Globals.appName,
                MessageBoxButton.OK, MessageBoxImage.Warning);
            return;
        }
    }
}
